package hou.fun.DownloadGirls;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author houweitao
 * @date 2016年11月5日下午3:15:42
 */

public class DownloadSexyChinese {
	private static final Logger log = LoggerFactory.getLogger(DownloadSexyChinese.class);
	private static int trySeedNum = 0;
	private static int tryPageNum = 0;
	private static int pageNum = 0;
	private static int girlNum = 0;
	private static int photoNum = 0;
	private static List<String> fails;
	private static List<String> tmp = new ArrayList<>();

	public static void main(String[] args) {
		DownloadSexyChinese ooxx = new DownloadSexyChinese();
		ooxx.init();
	}

	private void init() {
		String url =
		// "http://www.sexychinese.net/page/";
		"http://www.sexychinese.net/category/tuigirl/page/";
		for (pageNum = 1; pageNum < 200; pageNum++) {
			String cur = url + pageNum;
			System.out.println("正在下载:" + cur);
			crawlerSeed(cur);
		}

		while (tmp.size() > 0) {
			fails = tmp;
			tmp = new ArrayList<>();
			for (String str : fails) {
				download(str);
			}
		}
	}

	void crawlerSeed(String url) {
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).timeout(10000)
					.userAgent(
							"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
					.get();

			Elements girls = doc.select("div.post-thumbnail");

			for (Element girl : girls) {
				Element href = girl.select("a[href]").first();
				String page = href.attr("href");
				System.out.println(page);
				crawlerSinglePage(page);
			}
			trySeedNum = 0;
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("重新下载.." + url);

			if (trySeedNum++ < 5)
				crawlerSeed(url);
			else
				trySeedNum = 0;
		}

	}

	private void crawlerSinglePage(String url) {
		girlNum++;
		try {
			Document doc = Jsoup.connect(url).timeout(10000).followRedirects(true)
					.userAgent(
							"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
					.get();

			Elements images = doc.select(".entry-inner").select(".lazy.lazy-hidden");

			for (Element image : images) {
				String photo = image.attr("data-src");
				// System.out.println(photo);
				photoNum++;
				download(photo);
			}
			tryPageNum = 0;
		} catch (IOException e) {
			e.printStackTrace();
			if (tryPageNum++ < 5)
				crawlerSeed(url);
			else
				tryPageNum = 0;
		}

	}

	private void download(String photo) {
		try {
			// 即来即走，提高效率 2016年11月05日19:08:03
			new Thread(new Down(photo, this)).start();
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class Down implements Runnable {
		String photo;
		DownloadSexyChinese sexy;

		public Down(String photo, DownloadSexyChinese sexy) {
			this.photo = photo;
			this.sexy = sexy;
		}

		@Override
		public void run() {
			Sinagl singal = new Sinagl();
			PhotoDownload photoDown = new PhotoDownload(photo, sexy, singal);
			Thread down = new Thread(photoDown);
			down.start();

			// 30秒下载不下来就认为失败
			for (int i = 1; i < 300; i++) {
				try {
					if (singal.isFinished()) {
						return;
					}
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			log.info("time out, jump out...");
			tmp.add(photo);
			log.info("fail number: " + tmp.size());
			down.interrupt();
			return;
		}

	}

	// private void download(String photo) {
	// Sinagl singal = new Sinagl();
	// PhotoDownload photoDown = new PhotoDownload(photo, this, singal);
	// Thread down = new Thread(photoDown);
	// down.start();
	//
	// // 10秒下载不下来就认为失败
	// for (int i = 1; i < 100; i++) {
	// try {
	// if (singal.isFinished()) {
	// return;
	// }
	// Thread.sleep(100);
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }
	// log.info("time out, jump out...");
	// down.interrupt();
	// return;
	// }

	public int getPageNum() {
		return pageNum;
	}

	public int getPhotoNum() {
		return photoNum;
	}

	public int getGirlNum() {
		return girlNum;
	}

}
